package com.example.addressbook;

import android.widget.ImageView;
import android.widget.TextView;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.content.DialogInterface;
import android.database.Cursor;
import android.content.ContentUris;   
import android.view.ContextMenu;
import android.view.MenuItem;
import android.provider.DocumentsContract; 
import android.os.Build; 
import android.content.pm.PackageManager;  
import android.content.ContentResolver;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.ActivityCompat;
import android.content.pm.PackageManager; 
import android.Manifest;
import android.widget.Button;
import org.litepal.crud.DataSupport;

import java.util.*;
import java.io.*;

public class MainActivity extends AppCompatActivity {
	
	private ViewPager viewPager;
	private TabLayout tabLayout;
	
	private List<String> titles = new ArrayList<>();
	public List<Fragment> fragments = new ArrayList<>();
	
	public FragmentAdapter fragmentAdapter;
	
	public void init1(){
		tabLayout = (TabLayout)findViewById(R.id.tab_layout);
		viewPager = (ViewPager)findViewById(R.id.view_pager);
		titles.add("拨号");
		titles.add("联系人");
		titles.add("收藏");
		for(int i=0; i<titles.size(); i++){
			tabLayout.addTab(tabLayout.newTab().setText(titles.get(i)));
		}
		fragments.add(new ListFragment1());
		fragments.add(new ListFragment2());
		fragments.add(new ListFragment3());
		fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),fragments,titles);
		
		viewPager.setAdapter(fragmentAdapter);
		viewPager.setOffscreenPageLimit(2);
		tabLayout.setupWithViewPager(viewPager);
		tabLayout.setTabsFromPagerAdapter(fragmentAdapter);
			
	}
	
	public void init(){
		if( ContextCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE)
				!= PackageManager.PERMISSION_GRANTED ){
			ActivityCompat.requestPermissions(this,
				new String[]{Manifest.permission.CALL_PHONE},3);	
		}else init1();
	}
	
	@Override
	public void onCreate(Bundle savedIntanceState){
		super.onCreate(savedIntanceState);
		setContentView(R.layout.main_activity);
		if( ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CALL_LOG)
					!= PackageManager.PERMISSION_GRANTED ){
			ActivityCompat.requestPermissions(this,
				new String[]{Manifest.permission.READ_CALL_LOG},2);
		}else init();
	}
	
	@Override
	public void onActivityResult(int rquestCode,int resultCode,Intent intent){
		switch(rquestCode){
			case 1:
				fragmentAdapter.notifyDataSetChanged();
				if( resultCode==RESULT_OK){
					fragmentAdapter.notifyDataSetChanged();
					//Toast.makeText(this,"return MainActivity",Toast.LENGTH_SHORT).show();
				}
				break;		
				
			case 2:
				if( resultCode==RESULT_OK ){
					boolean oldState = intent.getBooleanExtra("old_state",false);
					boolean isCheck = intent.getBooleanExtra("is_check",false);
					String name = intent.getStringExtra("people_name");
					if( oldState==false && isCheck==true ){
						List<People> list = DataSupport.where("name = ?",name).find(People.class);
						People p = list.get(0);
						Friend f = new Friend(p.getName(),p.getTelephone(),
							p.getInformation(),p.getPicturePath());
						f.save();
					}else if( oldState==true && isCheck==false ){
						DataSupport.deleteAll(Friend.class,"name = ?",name);
					}
					fragmentAdapter.notifyDataSetChanged();
				}
				break;
	
			case 3:
				//Toast.makeText(this,"beforeeeeeeeeeeeee",Toast.LENGTH_SHORT).show();
			
				fragmentAdapter.notifyDataSetChanged();
				if( RESULT_OK==resultCode ){
					Toast.makeText(this,"afterfafga",Toast.LENGTH_SHORT).show();
					fragmentAdapter.notifyDataSetChanged();
				}
			default:
		}
	}
	
	@Override
	public void onRequestPermissionsResult(int requestCode,String [] permissions,int[] grantResults){
		switch(requestCode){
			case 1:
				break;
			case 2:
				if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ){
					init();
				}else{
					finish();
				}
				break;
			case 3:
				if(grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ){
					init1();
				}else{
					finish();
				}
		}
	}
}